package com.store.app.activity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.BookedViewPagerAdapter;
import com.store.app.fragment.Task1Fragment;
import com.store.app.fragment.Task2Fragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务
 */
public class TaskActivity extends FragmentActivity implements View.OnClickListener{
    public List<Fragment> fragments = new ArrayList<Fragment>();
    private TextView title;
    private ViewPager viewpager;
    private String showFragmentIndex = "0";
    private TextView tv1,tv2,tv3,tv4,tv5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_task);
        findView();
        setListener();
        initData();

    }


    private void setListener() {
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });



    }

    private void findView() {
//		radioGroup = (RadioGroup) findViewById(R.id.booked_rg);
//		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//
//			@Override
//			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				switch (checkedId) {
//					case R.id.booked_rb1:
//						viewpager.setCurrentItem(0);
//						break;
//					case R.id.booked_rb2:
//						viewpager.setCurrentItem(1);
//						break;
//					case R.id.booked_rb3:
//						viewpager.setCurrentItem(2);
//						break;
//					default:
//						break;
//				}
//
//			}
//		});

        viewpager = (ViewPager) findViewById(R.id.booked_viewpager);


        tv1 = (TextView) findViewById(R.id.tv_task1);
        tv2 = (TextView) findViewById(R.id.tv_task2);
        tv3 = (TextView) findViewById(R.id.tv_task3);
        tv4 = (TextView) findViewById(R.id.tv_task4);
        tv5 = (TextView) findViewById(R.id.tv_task5);
        title = (TextView) findViewById(R.id.tvTitle);
        title.setText("任务");
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
    }

    private void refreshUITab(String index) {
        if (index.equals("0")) {
            tv1.setTextColor(Color.parseColor("#f46534"));
            tv1.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tv2.setTextColor(Color.parseColor("#666666"));
            tv2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv3.setTextColor(Color.parseColor("#666666"));
            tv3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv4.setTextColor(Color.parseColor("#666666"));
            tv4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv5.setTextColor(Color.parseColor("#666666"));
            tv5.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        } else if (index.equals("1")) {
            tv1.setTextColor(Color.parseColor("#666666"));
            tv1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv2.setTextColor(Color.parseColor("#f46534"));
            tv2.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tv3.setTextColor(Color.parseColor("#666666"));
            tv3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv4.setTextColor(Color.parseColor("#666666"));
            tv4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv5.setTextColor(Color.parseColor("#666666"));
            tv5.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }else if (index.equals("2")) {
            tv1.setTextColor(Color.parseColor("#666666"));
            tv1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv2.setTextColor(Color.parseColor("#666666"));
            tv2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv3.setTextColor(Color.parseColor("#f46534"));
            tv3.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tv4.setTextColor(Color.parseColor("#666666"));
            tv4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv5.setTextColor(Color.parseColor("#666666"));
            tv5.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }else if (index.equals("3")) {
            tv1.setTextColor(Color.parseColor("#666666"));
            tv1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv2.setTextColor(Color.parseColor("#666666"));
            tv2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv3.setTextColor(Color.parseColor("#666666"));
            tv3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv4.setTextColor(Color.parseColor("#f46534"));
            tv4.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tv5.setTextColor(Color.parseColor("#666666"));
            tv5.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }else if (index.equals("4")) {
            tv1.setTextColor(Color.parseColor("#666666"));
            tv1.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv2.setTextColor(Color.parseColor("#666666"));
            tv2.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv3.setTextColor(Color.parseColor("#666666"));
            tv3.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv4.setTextColor(Color.parseColor("#666666"));
            tv4.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tv5.setTextColor(Color.parseColor("#f46534"));
            tv5.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        }
    }

    private void initData() {
        //新任务
        fragments.add(new Task1Fragment());
         //待提交
        Task2Fragment task2Fragment = new Task2Fragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", "submitted");
        task2Fragment.setArguments(bundle);
        fragments.add(task2Fragment);
        //待审核
        Task2Fragment task3Fragment = new Task2Fragment();
        Bundle bundle3 = new Bundle();
        bundle3.putString("status", "audited");
        task3Fragment.setArguments(bundle3);
        fragments.add(task3Fragment);

        //待结算
        Task2Fragment task4Fragment = new Task2Fragment();
        Bundle bundle4 = new Bundle();
        bundle4.putString("status", "settled");
        task4Fragment.setArguments(bundle4);
        fragments.add(task4Fragment);

        //已完成
        Task2Fragment task5Fragment = new Task2Fragment();
        Bundle bundle5 = new Bundle();
        bundle5.putString("status", "closed");
        task5Fragment.setArguments(bundle5);
        fragments.add(task5Fragment);

        BookedViewPagerAdapter disfratab = new BookedViewPagerAdapter(getSupportFragmentManager(), viewpager, fragments);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshUITab(position + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        refreshUITab(showFragmentIndex);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_task1:
                showFragmentIndex = "0";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv_task2:
                showFragmentIndex = "1";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(1);
                break;
            case R.id.tv_task3:
                showFragmentIndex = "2";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(2);
                break;
            case R.id.tv_task4:
                showFragmentIndex = "3";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(3);
                break;
            case R.id.tv_task5:
                showFragmentIndex = "4";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(4);
                break;
        }
    }



}